package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.UserDao;
import com.itheima.domain.User;
import com.itheima.service.UserService;
import com.itheima.utils.MD5Util;
import com.itheima.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;
import java.util.stream.Stream;

public class UserServiceImpl implements UserService {
    @Override
    public PageInfo<User> findByPage(int currentPage, int pageSize) {
        SqlSession sqlSession = null;
        PageInfo<User> info = null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            UserDao userDao = MybatisUtil.getMapper(sqlSession, UserDao.class);
            PageHelper.startPage(currentPage,pageSize);
            List<User> list=userDao.findAll();
            info = new PageInfo<>(list);
        } finally {
            MybatisUtil.close(sqlSession);
        }

        return info;
    }

    @Override
    public void save(User user) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            UserDao userDao = MybatisUtil.getMapper(sqlSession, UserDao.class);
            String id = UUID.randomUUID().toString();
            user.setId(id);
            String password = MD5Util.md5(user.getPassword());
            user.setPassword(password);
            userDao.save(user);
            MybatisUtil.commit(sqlSession);
        } finally {
            MybatisUtil.close(sqlSession);
        }

    }

    @Override
    public User findById(String id) {
        SqlSession sqlSession = null;
        User user= null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            UserDao userDao = MybatisUtil.getMapper(sqlSession, UserDao.class);
            user = userDao.findById(id);
            MybatisUtil.commit(sqlSession);
        } finally {
            MybatisUtil.close(sqlSession);
        }
        return user;
    }

    @Override
    public void update(User user) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            UserDao userDao = MybatisUtil.getMapper(sqlSession, UserDao.class);

            userDao.update(user);
            MybatisUtil.commit(sqlSession);
        } finally {
            MybatisUtil.close(sqlSession);
        }

    }

    @Override
    public void delete(String[] ids) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            UserDao userDao = MybatisUtil.getMapper(sqlSession, UserDao.class);
            Stream.of(ids).forEach(id->{
                    userDao.deleteRoleAssociationByUserId(id);
                    userDao.delete(id);});
            MybatisUtil.commit(sqlSession);
        } finally {
            MybatisUtil.close(sqlSession);
        }
    }

    @Override
    public List<User> findAll() {
        SqlSession sqlSession = null;
        List<User> list = null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            UserDao userDao = MybatisUtil.getMapper(sqlSession, UserDao.class);
            list=userDao.findAll();

        } finally {
            MybatisUtil.close(sqlSession);
        }

        return list;
    }
}
